package yxy.game.pm2.command.plainsarena;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsArenaMessage;
import yxy.game.pm2.module.plainsarena.PlainsArenaPlayerModule;

/** 中原擂台 购买挑战次数 */
@MessageType(proto = Command.PlainsArenaFormatTimesDeduct, classtype = PlainsArenaMessage.PlainsArenaFormatTimesDeductRequest.class)
public class CMD_PlainsArenaFormatTimesDeduct extends Command<PlainsArenaMessage.PlainsArenaFormatTimesDeductRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsArenaMessage.PlainsArenaFormatTimesDeductRequest request) {
		String playerCode = messageSource.getTag();

		PlainsArenaMessage.PlainsArenaFormatTimesDeductResponse.Builder response = PlainsArenaMessage.PlainsArenaFormatTimesDeductResponse.newBuilder();

		try {
			PlainsArenaPlayerModule module = PlainsArenaPlayerModule.get(playerCode);
			if (module == null) {
				send(messageSource, response, Result.ModuleNotOpen);// 模块没有开启
				return;
			}

			module.setUsedFormatTimes(1);
			module.set();

			response.setIsFormatInit(module.getIsFormatInit());
			response.setFormatTimes(PlainsArenaPlayerModule.getFormatTimes(module));
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void send(MessageSource messageSource, PlainsArenaMessage.PlainsArenaFormatTimesDeductResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}